#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

int main()
{
    umask(0);
    // c语言库函数封装的w
    //int fd = open("log.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
    // c语言库函数封装的a，系统调用怎么实现
    //int fd = open("log.txt", O_CREAT | O_WRONLY | O_APPEND, 0666);
    //close(fd);
    
    // 系统写入文件
    int fd = open("log.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
        if(-1 == fd)
      {
          perror("open fail!");
          _exit(1);
      }
         int cnt = 5;
        const char* buffer = "Hello World!\n";
            while(cnt--){
                write(fd, buffer, strlen(buffer));
            }
    close(fd);

    // 系统读取文件
    //int fd = open("log.txt", O_RDONLY);
    //if(-1 == fd)
    //{
    //    perror("open fail!");
    //    _exit(1);
    //}
    //const char* msg = "Hello World!\n";
    //char buffer[1024];
    //while(1)
    //{
    //   ssize_t s =  read(fd, buffer, strlen(msg));
    //   if(s > 0){
    //       printf("%s", buffer);
    //   }
    //   else{
    //       break;
    //   }
    //}
    
    // 探究fd
   // int fd1 = open("log1.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
   // int fd2 = open("log2.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
   // int fd3 = open("log3.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
   // 
   // printf("fd1:%d\n", fd1);
   // printf("fd2:%d\n", fd2);
   // printf("fd3:%d\n", fd3);

   // close(fd1);
   // close(fd2);
   // close(fd3);
    return 0;
}
